<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改' : '新增'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      :label-position="labelPosition"
      label-width="100px"
    >
      <el-row :gutter="24">
        <el-col>
          <el-form-item label="称谓">
            <el-select clearable style="width: 100%" v-model="form.appellation" placeholder="请选择">
              <el-option
                v-for="(item,index) in nameArr"
                :key="index"
                :label="item.title"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="姓名">
            <el-input v-model="form.name" placeholder="请输入姓名" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="政治面貌">
            <el-select
              clearable
              style="width: 100%"
              v-model="form.politicalStatus"
              placeholder="请选择"
            >
              <el-option
                v-for="(item,index) in faceArr"
                :key="index"
                :label="item.title"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="身份证号" prop="idCards">
            <el-input v-model="form.idCards" placeholder="请输入身份证号码" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="工作单位">
            <el-input v-model="form.workUnits" placeholder="请输入工作单位" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="手机号码" prop="phone">
            <el-input v-model.number="form.phone" placeholder="请输入手机号码" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleUpdate">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateUserFamily, createUserFamily } from '@/api/archives'
import { openNotification, getSession, getDicData, checkPhone } from '@/utils/util'

export default {
  data() {
    return {
      isEdit: false,
      visible: false,
      labelPosition: 'right',
      form: {},
      inputWidth: {
        width: '100%'
      },
      rules: {
        idCards: [
          { pattern: /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/, message: '证件号码格式有误', trigger: 'blur' }
        ],
        phone: [{ type: 'number', validator: checkPhone, trigger: 'blur' }]
      },
      faceArr: [],
      nameArr: [],
      leaderRow: JSON.parse(this.$route.query.leaderRow)
    }
  },
  methods: {
    edit(bool, obj) {
      this.isEdit = bool
      this.visible = true
      this.form = Object.assign({}, obj)
      this.$set(this.form, 'userId', this.leaderRow.userId)
    },
    handleUpdate() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = null,
            mes = null
          if (this.isEdit) {
            res = await updateUserFamily(this.form)
            mes = '修改操作成功'
          } else {
            res = await createUserFamily(this.form)
            mes = '新增操作成功'
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  },
  created() {
    this.faceArr = getDicData('001', '001006')
    this.nameArr = getDicData('001', '001014')
  }
}
</script>
